package com.haredot.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.haredot.entity.Course;
import com.haredot.entity.Mark;
import com.haredot.entity.Student;
import com.haredot.entity.Teacher;
import com.haredot.mapper.CourseMapper;
import com.haredot.mapper.MarkMapper;
import com.haredot.mapper.StudentMapper;
import com.haredot.mapper.TeacherMapper;
import com.haredot.service.TeacherService;
import com.haredot.vo.LayuiPage;
import com.haredot.vo.R;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
@Transactional(rollbackFor = Exception.class)
public class TeacherServiceImpl implements TeacherService {

    @Resource
    private CourseMapper courseMapper ;

    @Resource
    private TeacherMapper teacherMapper;

    @Resource
    private StudentMapper studentMapper ;

    @Resource
    private MarkMapper markMapper ;

    @Override
    public R<Course> saveCourse(Course course) {

        Course cs = courseMapper.findCourseByName(course.getName());

        if (cs != null) {
            return R.<Course>fail().data(Map.of("name", "课程名已经存在"));
        }
        courseMapper.insert(course);
        return R.<Course>ok().data(course);
    }

    @Override
    public R<Page<Course>> queryCourseList(LayuiPage layuiPage) {
        // 分页查询所有的学生信息
        Page<Course> page = PageHelper.startPage(layuiPage)
                .doSelectPage(() -> courseMapper.findAll());

        return R.<Page<Course>>ok().data(page);
    }

    @Override
    public R<?> batchCourseRemove(List<Integer> ids) {
        courseMapper.batchRemove(ids);
        return R.ok().build();
    }

    @Override
    public R<Course> queryCourseById(Integer id) {
        return R.<Course>ok().data(courseMapper.selectByPrimaryKey(id));
    }

    @Override
    public R<Course> modifyCourse(Course course) {
        Course cs = courseMapper.findCourseByName(course.getName());

        if (cs != null && !Objects.equals(cs.getId(), course.getId())) {
            return R.<Course>fail().data(Map.of("name", "课程名已经存在"));
        }
        courseMapper.updateByPrimaryKey(course);
        return R.<Course>ok().data(course);
    }

    @Override
    public R<?> removeCourse(Integer id) {
        courseMapper.deleteByPrimaryKey(id);
        return R.ok().build();
    }

    @Override
    public List<Course> queryCourseList() {
        return courseMapper.findAll();
    }

    @Override
    public R<Teacher> save(Teacher teacher) {
        // 查询学号是否已存在
        Teacher stu = teacherMapper.selectByPrimaryKey(teacher.getId());

        if (stu != null) return R.<Teacher>fail().data(Map.of("id", "工号已存在"));

        teacherMapper.insert(teacher);

        return R.<Teacher>ok().data(teacher);
    }

    @Override
    public R<Page<Teacher>> queryTeacherList(LayuiPage layuiPage) {
        Page<Teacher> page = PageHelper.startPage(layuiPage)
                .doSelectPage(() -> teacherMapper.findAll());

        return R.<Page<Teacher>>ok().data(page);
    }

    @Override
    public R<?> batchRemove(List<Long> ids) {
        teacherMapper.batchRemove(ids);
        return R.ok().build();
    }

    @Override
    public R<?> remove(Long id) {
        teacherMapper.deleteByPrimaryKey(id);
        return R.ok().build();
    }

    @Override
    public R<Teacher> queryTeacherById(Long sno) {
        Teacher teacher = teacherMapper.selectByPrimaryKey(sno);
        return R.<Teacher>ok().data(teacher);
    }

    @Override
    public R<Teacher> modify(Teacher teacher) {
        teacherMapper.updateByPrimaryKey(teacher);
        return R.<Teacher>ok().data(teacher);
    }

    @Override
    public R<Course> checkSnoAndQueryCourse(Long sno) {
        // 查询 输入的学号是否存在
        Student student = studentMapper.selectByPrimaryKey(sno);

        if (student == null) return R.<Course>fail("学号不存在").build();

        // 查找该学生选课信息
        List<Course> courses = courseMapper.findCoursesBySno(sno);

        if (courses.isEmpty()) return R.<Course>fail("该学号对应的学生没有选择任何课程").build();

        return R.<Course>ok().data(courses);
    }

    @Override
    public R<?> saveMark(Mark mark) {

        Mark ms = markMapper.findMarkBySnoAndCourseId(mark.getStuId(), mark.getCourseId());

        if (ms != null) {
            return R.fail().data(Map.of("name", "该学生这门课程成绩已录入"));
        }
        markMapper.insert(mark);
        return R.<Course>ok().build() ;
    }

    @Override
    public R<Page<Map<String, Object>>> queryMarksList(LayuiPage layuiPage, Map<String, Object> params) {

        Page<Map<String, Object>> page = PageHelper.startPage(layuiPage)
                .doSelectPage(() -> markMapper.findMarkListByCondition(params));

        return R.<Page<Map<String, Object>>>ok().data(page);
    }

    @Override
    public R<?> removeMark(Integer id) {
        markMapper.deleteByPrimaryKey(id);
        return R.ok().build() ;
    }

    @Override
    public R<?> batchMarkRemove(List<Integer> ids) {
        markMapper.batchRemove(ids);
        return R.ok().build();
    }

    @Override
    public R<Map<String, Object>> queryMarkById(Integer id) {
        // 查询 mark 的基本信息
        Mark mark = markMapper.selectByPrimaryKey(id);
        // 查询对应学号 选修的课程
        List<Course> courses = courseMapper.findCoursesBySno(mark.getStuId());

        Map<String, Object> map = new HashMap<>();
        map.put("mark", mark);
        map.put("courses", courses);

        return R.<Map<String, Object>>ok().data(map);
    }

    @Override
    public R<?> modifyMark(Mark mark) {
        Mark ms = markMapper.findMarkBySnoAndCourseId(mark.getStuId(), mark.getCourseId());

        if (ms != null && Objects.equals(mark.getCourseId(), ms.getCourseId()) && !Objects.equals(mark.getId(), ms.getId())) {
            return R.fail().data(Map.of("name", "该学生这门课程成绩已录入"));
        }

        markMapper.updateByPrimaryKey(mark);
        return R.ok().build();
    }
}
